package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.ModifyGenderContract;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class ModifyGenderPresenter implements ModifyGenderContract.Presenter {

    private ModifyGenderContract.View mContractView;

    private CompositeDisposable disposable;

    public ModifyGenderPresenter(ModifyGenderContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getGender(final String gender) {
        Request.getIntence().setGender(StorageHelper.getToken(),gender)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            ResUserInfoEntity resUserInfoEntity = ParseUtil.parseObject(StorageHelper.getAccountInfo(), ResUserInfoEntity.class);
                            resUserInfoEntity.setSex(gender);
                            StorageHelper.saveAccountInfo(ParseUtil.toJson(resUserInfoEntity));
                            mContractView.setGender(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
